<?php
        
        $valores = array();
        
        $file_handle = fopen("conf.txt", "r");
        $line = split(":",fgets($file_handle));

        $hostname= $line[0];
        $db_user= $line[1];
        $db_pass= $line[2];
        $db_name= chop($line[3]);
        
        $line = split("=",fgets($file_handle));
        
        $months_default= chop($line[1]);

        fclose($file_handle);
        
       $con = mysql_connect($hostname,$db_user,$db_pass);
       
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        date_default_timezone_set('UTC');
        mysql_select_db($db_name, $con);
                   
                    
        if ($_GET["param"] == 'dod_manual'){
            if (isset($_GET["desde"]) && isset($_GET["hasta"]))
            {
                $sql_ibatd = mysql_query('SELECT fecha, '.$_GET["tipo"].' FROM ibatd WHERE fecha > "'.$_GET['desde'].'" AND fecha < "'.$_GET['hasta'].'";');
                $sql_ibatc = mysql_query('SELECT fecha, '.$_GET["tipo"].' FROM ibatc WHERE fecha > "'.$_GET['desde'].'" AND fecha < "'.$_GET['hasta'].'";');
            }
            else{
                $time = strtotime("today");
                $month_ago = date("Y-m-d", strtotime("-".$months_default. "month", $time));
                $sql_ibatd = mysql_query('SELECT fecha, '.$_GET["tipo"].' FROM ibatd WHERE fecha > "'.$month_ago.'";');
                $sql_ibatc = mysql_query('SELECT fecha, '.$_GET["tipo"].' FROM ibatc WHERE fecha > "'.$month_ago.'";');
                
            }
            $inicio = TRUE;
            $sql =$sql_ibatd;
            $sql_x = $sql_ibatc;
            $sql_flag = 'ibatd';
            while($current_row = mysql_fetch_array($sql))
            {
                if ($inicio){
                    $inicio = FALSE;
                    $other_row = mysql_fetch_array($sql_x);
                    if (strtotime($current_row['fecha']) - strtotime($other_row['fecha']) <= 0){
                        $delta_q_i = 0/*floatval($current_row['RPV'])*/;
                        $fecha_i = date_create($current_row['fecha']);
                    }
                    else{
                        $delta_q_i = 0/*floatval($other_row['RPV'])*/;
                        $fecha_i = date_create($other_row['fecha']);
                        $other_row = $current_row;
                        $sql = $sql_ibatc;
                        $sql_x =$sql_ibatd;
                        $sql_flag = 'ibatc';
                    }
                    
                }
                else{
                    
                     if (strtotime($current_row['fecha']) - strtotime($other_row['fecha']) <= 0){
                         //echo 'current';
                        $intervalo = date_diff(date_create($current_row['fecha']), $fecha_i);
                        
                         if ($sql_flag == 'ibatd'){
                            $delta_q_i += floatval($current_row['RPV'])*intval($intervalo->format('%s'));
                        }
                        else{
                            $delta_q_i -= floatval($current_row['RPV'])*intval($intervalo->format('%s'));
                        }
                        
                        $dod = $delta_q_i/(15.18*3600)*100;
                        if ($dod > 0){
                            array_push($valores, array(strtotime($current_row['fecha'])*1000, floatval($dod)));
                        }
                        else{
                            array_push($valores, array(strtotime($current_row['fecha'])*1000, floatval(0)));
                        }
                        $fecha_i = date_create($current_row['fecha']);
                    }
                    else{
                        $intervalo = date_diff(date_create($other_row['fecha']), $fecha_i);
                        if ($sql == $sql_ibatd){
                            $sql = $sql_ibatc;
                            $sql_x = $sql_ibatd;
                            $sql_flag = 'ibatc';
                        }
                        else{
                            $sql = $sql_ibatd;
                            $sql_x = $sql_ibatc;
                            $sql_flag = 'ibatd';
                        }
                        
                        $dod = $delta_q_i/(15.18*3600)*100;
                        if ($dod > 0){
                            array_push($valores, array(strtotime($other_row['fecha'])*1000, floatval($dod)));
                        }
                        else{
                            array_push($valores, array(strtotime($other_row['fecha'])*1000, floatval(0)));
                        }
                        $fecha_i = date_create($other_row['fecha']);
                        $other_row = $current_row;
                    }
                }
            }
        }
        else{
            
            if ($_GET["modo"] == "max_min"){
                $inicio = TRUE;
                if (isset($_GET["desde"]) && isset($_GET["hasta"]))
                {
                    $sql = mysql_query('SELECT fecha,' .$_GET["tipo"]. ' FROM '.$_GET["param"].' WHERE fecha > "'.$_GET['desde'].'" AND fecha < "'.$_GET['hasta'].'";');
                }
                else{
                    $time = strtotime("now");
                    $month_ago = date("Y-m-d", strtotime("-".$months_default. "month", $time));
                    $sql = mysql_query('SELECT fecha,' .$_GET["tipo"]. ' FROM '.$_GET["param"].' WHERE fecha > "'.$month_ago.'";');
                    }

                $values = 0;
                $currentDay = "000000000000";
                $first = TRUE;

                $num_rows = mysql_num_rows($sql);
                
                while($row = mysql_fetch_array($sql))
                {
                    $values += 1;
                    if ( date_format(date_create($currentDay),'Y-m-d') != date_format(date_create($row['fecha']), 'Y-m-d') || ($values == $num_rows) ){

                        if (!$first)
                            {

                                array_push($valores, array(strtotime(date_format(date_create($currentDay),'Y-m-d'))*1000,$min,$max));
                                $first = TRUE;
                            }
                        $max = floatval($row[$_GET["tipo"]]);
                        $min = floatval($row[$_GET["tipo"]]);
                        
                        $currentDay = $row['fecha'];
                        $first = FALSE;

                    }
                    else{
                            if (floatval($row[$_GET["tipo"]]) > $max){
                                $max = floatval($row[$_GET["tipo"]]);
                                
                            }
                            if (floatval($row[$_GET["tipo"]]) < $min){
                                $min = floatval($row[$_GET["tipo"]]);
                            }        
                    }

                }
             
            }
            
            else{ //"modo normal"
                
                if (isset($_GET["desde"]) && isset($_GET["hasta"]))
                {
                    $sql = mysql_query('SELECT fecha,' .$_GET["tipo"]. ' FROM '.$_GET["param"].' WHERE fecha > "'.$_GET['desde'].'" AND fecha < "'.$_GET['hasta'].'";');
                }
                else{
                    
                    $time = strtotime("now");
                    $month_ago = date("Y-m-d", strtotime("-".$months_default. "month", $time));
                    $sql = mysql_query('SELECT fecha,' .$_GET["tipo"]. ' FROM '.$_GET["param"].' WHERE fecha > "'.$month_ago.'";');
                    }
                while($row = mysql_fetch_array($sql))
                {
                    array_push($valores, array(strtotime($row['fecha'])*1000, floatval($row[$_GET["tipo"]])));

                }
            }
        }
                    
        mysql_close($con);
        echo json_encode($valores);
?>